home *** CD-ROM | disk | FTP | other *** search
- Path: grimsel.zurich.ibm.com!usenet
- From: Keith Whittingham <wgk@zurich.ibm.com>
- Newsgroups: comp.lang.c++
- Subject: Re: HELP Borland C++ 5.0 pointer problem
- Date: Wed, 17 Apr 1996 09:44:52 -0700
- Organization: IBM Zurich Research Laboratory
- Message-ID: <31752004.62B1@zurich.ibm.com>
- References: <4klke8$7mb@dub-news-svc-2.compuserve.com> <316e9fdb.457757@10.0.2.1> <317409DC.59B@ids2.idsonline.com>
- NNTP-Posting-Host: pine.zurich.ibm.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (Win16; I)
-
- Lally Singh wrote:
- >>
- > char * testfunction( char * inData)
- > {
- > char * buffer = new[] char[20]; // allocates on the heap, remember to delete[] it!
- > strcpy( buffer, inData ); // copy the string
- > return buffer; // return the new buffer
- > };
-
- Although that will work I would suggest that it's bad programming
- practice: The function which allocates memory should be the
- function which deletes it. For instance if I decided that
- I could replace your function with one line...
-
- char *testfunction(char *inData)
- {
- return strdup(inData);
- }
-
- Which is perfectly valid (smaller, and works faster) then
- we have a problem. If the caller deletes the
- memory we have a potential disaster on our hands.
- He should of course free() the block.
-
- The solution? Either provide an allocation function and a deletion
- function or have the caller pass you the ready allocated memory.
-
-
- --
- Keith Whittingham
- wgk@zurich.ibm.com
-